home *** CD-ROM | disk | FTP | other *** search
/ Robotics & Artificial Int…3 (Professional Edition) / Robotics & Artificial Intelligence Tools 2003 (Professional Edition).iso / neural network tool and application / nsinstall.exe / data1.cab / DLLCust_Files / PREPOST / DISCRIM.C < prev    next >
Encoding:
C/C++ Source or Header  |  2002-03-08  |  6.9 KB  |  193 lines

  1. // Dynamic link library implementation of a generic pre/post-processor
  2.  
  3. #include "NSDLL.h" 
  4. #include <windows.h>
  5.  
  6. #define matrix(i,j)    output[i + buffer.yOffset - j*buffer.steps]
  7.  
  8. typedef struct {
  9.     int steps; 
  10.     int currentX, currentY;
  11.     int xChannel, yChannel;
  12.     int plotChannel;
  13.     int yOffset;
  14.     int outputAxonLength;
  15.     boolean allChannelFlag;
  16.     NSFloat minX, maxX;
  17.     NSFloat minY, maxY;
  18.     NSFloat xInterval, yInterval;
  19.     NSFloat inputHoldValue;
  20. } BufferData;
  21.  
  22. BufferData buffer = {0, 0, 0, 0, 0, 0, 0, 0, FALSE, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f};
  23.  
  24. /*****************************************/
  25. /* Activation of Postprocessor component */
  26. __declspec(dllexport) BOOL performPrePost(
  27.     DLLData    *instance,    // Pointer to instance data (may be NULL)
  28.     NSFloat    *input,     // Pointer to the input data
  29.     NSFloat    *output,     // Pointer to the output data
  30.     int rows,         // Number of rows of data
  31.     int cols,         // Number of cols of output data, can be changed to reflect a diffenent number for the input data
  32.     BOOL preprocessor    // Flag to indicate whether this is a preprocessor or postprocessor
  33.     )
  34. {
  35.     if (buffer.allChannelFlag) {
  36.         int i, firstLargestOutputIndex=0, secondLargestOutputIndex=0;
  37.         NSFloat firstLargestOutput=input[0], secondLargestOutput=-1.0E-99f;
  38.         for (i=1;i<buffer.outputAxonLength;i++) {
  39.             if (input[i] > firstLargestOutput) {
  40.                 firstLargestOutputIndex = i;
  41.                 firstLargestOutput = input[i];
  42.             }
  43.             else if (input[i] > secondLargestOutput) {
  44.                 secondLargestOutputIndex = i;
  45.                 secondLargestOutput = input[i];
  46.             }
  47.         }
  48.         matrix(buffer.currentX, buffer.currentY) = input[secondLargestOutputIndex] - input[firstLargestOutputIndex] + 1;
  49.     }
  50.     else
  51.         matrix(buffer.currentX, buffer.currentY) = input[buffer.plotChannel];
  52.         
  53.     if (++buffer.currentX >= buffer.steps) {
  54.         buffer.currentX = 0;
  55.         if (++buffer.currentY >= buffer.steps) {
  56.             buffer.currentY = 0;
  57.             return TRUE;
  58.         }
  59.     }
  60.     return FALSE;    // Return whether to inject this sample or to call performPrePost with another sample
  61. }
  62.  
  63. /**********************************/
  64. /* Activation of output component */
  65. __declspec(dllexport) void performInput(
  66.     DLLData    *instance,    // Pointer to instance data (may be NULL)
  67.     NSFloat    *data,         // Pointer to the data
  68.     int rows,         // Number of rows of data
  69.     int cols         // Number of cols of data
  70.     )
  71. {
  72.     int i, length=rows*cols;
  73.     for (i=0;i<length;i++)
  74.         data[i] = buffer.inputHoldValue;
  75.     data[buffer.xChannel] = buffer.minX + buffer.currentX*buffer.xInterval;
  76.     data[buffer.yChannel] = buffer.minY + buffer.currentY*buffer.yInterval;
  77. }
  78.  
  79. /******************************************/
  80. /* Called every time the network is reset */
  81. __declspec(dllexport) void networkReset(
  82.     DLLData    *instance    // Pointer to instance data (may be NULL) 
  83.     )
  84. {
  85.     buffer.currentX = 0;
  86.     buffer.currentY = 0;
  87. }
  88.  
  89. /******************************************/
  90. /* Management of instance data (OPTIONAL) */
  91. __declspec(dllexport) DLLData *allocPrePost(
  92.     DLLData    *oldInstance,    // Pointer to the last instance if reallocating
  93.     int *rows,         // Number of rows of output data, can be changed to reflect a diffenent number for the input data
  94.     int *cols,         // Number of cols of output data, can be changed to reflect a diffenent number for the input data
  95.     BOOL preprocessor    // Flag to indicate whether this is a preprocessor or postprocessor
  96.     )
  97. {    
  98.     DLLData *instance = allocDLLInstance(oldInstance);
  99.     if (preprocessor)
  100.         MessageBox(NULL, "Discriminant Function DLL should only be used as a postprocessor", "Warning", MB_OK);
  101.     setParameterName(instance, 1, 1, "Steps", TRUE);
  102.     setIntParameter(instance, 1, 1, 20, FALSE);
  103.     setParameterName(instance, 2, 1, "All Channels", TRUE);
  104.     setBoolParameter(instance, 2, 1, FALSE, FALSE);
  105.     setParameterName(instance, 3, 1, "Plot Channel", TRUE);
  106.     setIntParameter(instance, 3, 1, 0, FALSE);
  107.     buffer.steps = getIntParameter(instance, 1, 1);
  108.     buffer.allChannelFlag = getBoolParameter(instance, 2, 1);
  109.     buffer.plotChannel = getIntParameter(instance, 3, 1);
  110.      if (buffer.steps < 2)
  111.         buffer.steps = 2;
  112.     if (buffer.plotChannel >= *rows * *cols)
  113.         buffer.plotChannel = *rows * *cols - 1;
  114.      setIntParameter(instance, 1, 1, buffer.steps, TRUE);
  115.     setIntParameter(instance, 3, 1, buffer.plotChannel, TRUE);
  116.     buffer.outputAxonLength = *rows * *cols;
  117.     if (buffer.outputAxonLength==1) {
  118.         buffer.allChannelFlag = FALSE;
  119.         setBoolParameter(instance, 2, 1, FALSE, FALSE);
  120.     }
  121.     *rows = *cols = buffer.steps;
  122.     buffer.xInterval = (buffer.maxX-buffer.minX)/(buffer.steps-1);
  123.     buffer.yInterval = (buffer.maxY-buffer.minY)/(buffer.steps-1);
  124.     buffer.yOffset = buffer.steps*(buffer.steps - 1);
  125.     networkReset(instance);
  126.     return instance;
  127. }
  128.  
  129. __declspec(dllexport) void freePrePost(DLLData *instance)
  130. {
  131.     freeDLLInstance(instance); 
  132. }
  133.  
  134. __declspec(dllexport) DLLData *allocInput(
  135.     DLLData    *oldInstance,    // Pointer to the last instance if reallocating
  136.     int rows,         // Number of rows of data
  137.     int cols         // Number of cols of data
  138.     )
  139. {
  140.     DLLData *instance = allocDLLInstance(oldInstance);
  141.     setParameterName(instance, 1, 0, "X Channel", TRUE);
  142.     setIntParameter(instance, 1, 0, 0, FALSE);
  143.     setParameterName(instance, 1, 1, "Min X", TRUE);
  144.     setFloatParameter(instance, 1, 1, -1.0f, FALSE);
  145.     setParameterName(instance, 1, 2, "Max X", TRUE);
  146.     setFloatParameter(instance, 1, 2, 1.0f, FALSE);
  147.     setParameterName(instance, 2, 0, "Y Channel", TRUE);
  148.     setIntParameter(instance, 2, 0, 1, FALSE);
  149.     setParameterName(instance, 2, 1, "Min Y", TRUE);
  150.     setFloatParameter(instance, 2, 1, -1.0f, FALSE);
  151.     setParameterName(instance, 2, 2, "Max Y", TRUE);
  152.     setFloatParameter(instance, 2, 2, 1.0f, FALSE);
  153.     setParameterName(instance, 3, 0, "Other Hold", TRUE);
  154.     setFloatParameter(instance, 3, 0, 0.0f, FALSE);
  155.     buffer.xChannel = getIntParameter(instance, 1, 0);
  156.     buffer.minX = getFloatParameter(instance, 1, 1);
  157.     buffer.maxX = getFloatParameter(instance, 1, 2);
  158.     buffer.yChannel = getIntParameter(instance, 2, 0);
  159.     buffer.minY = getFloatParameter(instance, 2, 1);
  160.     buffer.maxY = getFloatParameter(instance, 2, 2);
  161.     buffer.inputHoldValue = getFloatParameter(instance, 3, 0);
  162.     if (buffer.xChannel >= rows*cols) {
  163.         buffer.xChannel = rows*cols-1;
  164.         setIntParameter(instance, 1, 0, buffer.xChannel, TRUE);
  165.     }
  166.     if (buffer.yChannel >= rows*cols) {
  167.         buffer.yChannel = rows*cols-1;
  168.         setIntParameter(instance, 2, 0, buffer.yChannel, TRUE);
  169.     }
  170.     if (buffer.minX >= buffer.maxX) {
  171.         buffer.maxX = buffer.minX + 0.1f;
  172.         setFloatParameter(instance, 1, 2, buffer.maxX, TRUE);
  173.     }
  174.     if (buffer.minY >= buffer.maxY) {
  175.         buffer.maxY = buffer.minY + 0.1f;
  176.         setFloatParameter(instance, 1, 2, buffer.maxY, TRUE);
  177.     }
  178.     buffer.xInterval = (buffer.maxX-buffer.minX)/(buffer.steps-1);
  179.     buffer.yInterval = (buffer.maxY-buffer.minY)/(buffer.steps-1);
  180.     networkReset(instance);
  181.     return instance;
  182. }
  183.  
  184. __declspec(dllexport) void freeInput(DLLData *instance)
  185. {
  186.     freeDLLInstance(instance); 
  187. }
  188.  
  189. __declspec(dllexport) int exemplars(DLLData *instance)
  190. {
  191.     return buffer.steps * buffer.steps; 
  192. }
  193.